/**
 * Copyright 2012 Brigham Young University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package edu.byu.nlp.cluster;

import java.util.Collection;

import org.apache.commons.math3.random.RandomGenerator;

import com.google.common.collect.Lists;

import edu.byu.nlp.data.SparseFeatureVector;
import edu.byu.nlp.pipes.Instance;
import edu.byu.nlp.util.Collections3;
import edu.byu.nlp.util.Indexer;

/**
 * @author rah67
 *
 */
public class Dataset {

	private Collection<Instance<Integer, SparseFeatureVector>> labeledData;
	private Collection<Instance<Integer, SparseFeatureVector>> unlabeledData;
	private final Indexer<String> wordIndex;
	private final Indexer<String> labelIndex;

	public Dataset(Collection<Instance<Integer, SparseFeatureVector>> labeledData,
			Collection<Instance<Integer, SparseFeatureVector>> unlabeledData,
			Indexer<String> wordIndex,
			Indexer<String> labelIndex) {
		this.labeledData = labeledData;
		this.unlabeledData = unlabeledData;
		this.wordIndex = wordIndex;
		this.labelIndex = labelIndex;
	}

	public int getNumLabels() {
		return labelIndex.size();
	}
	
	public int getNumFeatures() { 
		return wordIndex.size();
	}

	public Collection<Instance<Integer, SparseFeatureVector>> labeledData() {
		return labeledData;
	}

	public Collection<SparseFeatureVector> unlabeledData() {
		return Datasets.unlabel(unlabeledData);
	}
	
	/** Use with caution; typically you'll want to use .unlabeledData(). This method is mainly for evaluation **/
	public Collection<Instance<Integer, SparseFeatureVector>> unlabledInstances() {
		return unlabeledData;
	}

	public void shuffle(RandomGenerator rnd) {
		labeledData = Collections3.shuffledCopyOf(labeledData, rnd);
		unlabeledData = Collections3.shuffledCopyOf(unlabeledData, rnd);
	}

	/** Moves the last n labeledItems to (the first part of) unlabeledData; consider shuffling first **/
	public void hideLabels(int n) {
		int newNumLabeled = labeledData.size() - n;
		unlabeledData = Collections3.concat(Collections3.skip(labeledData, newNumLabeled), unlabeledData);
		labeledData = Collections3.limit(labeledData, newNumLabeled);
	}

	/**
	 * Copies the collections into array lists; maintains reference to indexers.
	 * Useful after operations such as hideLabels and split.
	 */
	public Dataset copy() {
		return new Dataset(Lists.newArrayList(labeledData), Lists.newArrayList(unlabeledData), wordIndex, labelIndex);
	}

	public Indexer<String> getLabelIndex() {
		return labelIndex;
	}

	public int getNumTokens() {
		int count = 0;
		for (Instance<Integer, SparseFeatureVector> instance : labeledData()) {
			count += instance.getData().sum();
		}
		for (SparseFeatureVector instance : unlabeledData()) {
			count += instance.sum();
		}
		return count;
	}

}
